<template>
  <KView class="sys-star-input">
    <KView>
      <KView v-for="num in fullStar" :key="`fullstar_${num}`" :style="starSize" @click="starClick(num)">
        <sys-svg name="star" color="#898989"></sys-svg>
      </KView>
    </KView>
    <KView :style="starStyle">
      <KView v-for="num in fullStar" :key="`star_${num}`" :style="starSize" @click="starClick(num)">
        <sys-svg name="star" color="#ffa50b"></sys-svg>
      </KView>
    </KView>
  </KView>
</template>

<script>
  import SystemUtil from 'utils/systemUtil';

  import SysSvg from 'common/sysSvg';

  export default {
    props: {
      value: {
        type: Number,
        required: true
      },
      fullStar: {
        type: Number,
        default: 5
      },
      size: {
        type: Number,
        default: 20
      }
    },

    computed: {
      starStyle() {
        let size = this.size;
        let value = this.value;
        let width = value * (size + 10);
        return {
          width: SystemUtil.formatPx(width)
        };
      },

      starSize() {
        let size = this.size;
        let width = size + 10;
        let height = size;
        return {
          width: SystemUtil.formatPx(width),
          height: SystemUtil.formatPx(height)
        };
      }
    },

    data() {
      return {};
    },

    methods: {
      starClick(num) {
        this.$emit('input', num);
        this.$emit('change', num);
      }
    },

    components: {
      SysSvg
    }
  };

</script>

<style lang="scss">
  .sys-star-input {
    width: 100%;
    height: 100%;
    position: relative;

    >div {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      overflow: hidden;
      white-space: nowrap;

      >div {
        display: inline-block;
        vertical-align: top;
      }
    }
  }

</style>
